#include "stm32f4xx_hal.h"
#include "stdio.h"
#include "string.h"
#include "log.h"
#include "version.h"

#include "DevicesQueue.h"
#include "DevicesPower.h"

#include "DriverNetwork.h"
#include "DriverVoltage.h"
#include "DriverCurrent.h"
#include "DriverShell.h"


int8_t cShellJsonCallbackVersion(cJSON *pjsonJsonRoot)
{
    cJSON *pcJsonItem = NULL, *pcJsonItemNew = NULL, *pcJsonArray = NULL;
    int32_t iArraySize = 0, i = 0;

    if((pcJsonItem = cJSON_GetObjectItem(pjsonJsonRoot, "Get")) != NULL)
    {
        /* 新增元素 */
        pcJsonItemNew = cJSON_CreateObject();
        cJSON_AddItemToObject(pjsonJsonRoot, pcJsonItem->string, pcJsonItemNew);

        /* 遍历 */
        iArraySize = cJSON_GetArraySize(pcJsonItem);
        for(i = 0; i < iArraySize; ++i)
        {
            pcJsonArray = cJSON_GetArrayItem(pcJsonItem, i);

            if(strcasecmp(pcJsonArray->valuestring, "Hard version") == 0)
                cJSON_AddStringToObject(pcJsonItemNew, pcJsonArray->valuestring, HARD_VERSION);

            else if(strcasecmp(pcJsonArray->valuestring, "Soft version") == 0)
                cJSON_AddStringToObject(pcJsonItemNew, pcJsonArray->valuestring, SOFTWARE_VERSION);
        }

        /* 删除旧数组元素 */
        cJSON_DeleteItemFromObject(pjsonJsonRoot, pcJsonItem->string);
    }

    return 0;
}

int8_t cShellJsonCallbackPower(cJSON *pjsonJsonRoot)
{
    cJSON *pcJsonItem[2] = {NULL};
    VoltageInfoType *ptypeVoltageInfoHandle = ptypeVoltageInfoGet();

    if((pcJsonItem[0] = cJSON_GetObjectItem(pjsonJsonRoot, "Set")) != NULL)
    {
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "All")) != NULL)
        {
            if(strcasecmp(pcJsonItem[1]->valuestring, "DISABLE") == 0)
                vPowerClose(VOLTAGE_CHANNEL_ALL);
            else if(strcasecmp(pcJsonItem[1]->valuestring, "ENABLE") == 0)
                vPowerOpen(VOLTAGE_CHANNEL_ALL);
        }
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "COM")) != NULL)
        {
            if(strcasecmp(pcJsonItem[1]->valuestring, "DISABLE") == 0)
                vPowerClose(POWER_CHANNEL_COM);
            else if(strcasecmp(pcJsonItem[1]->valuestring, "ENABLE") == 0)
                vPowerOpen(POWER_CHANNEL_COM);
        }
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "VDD1")) != NULL)
        {
            if(strcasecmp(pcJsonItem[1]->valuestring, "DISABLE") == 0)
                vPowerClose(POWER_CHANNEL_VCC1);
            else if(strcasecmp(pcJsonItem[1]->valuestring, "ENABLE") == 0)
                vPowerOpen(POWER_CHANNEL_VCC1);
        }
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "VDD2H")) != NULL)
        {
            if(strcasecmp(pcJsonItem[1]->valuestring, "DISABLE") == 0)
                vPowerClose(POWER_CHANNEL_VCC2H);
            else if(strcasecmp(pcJsonItem[1]->valuestring, "ENABLE") == 0)
                vPowerOpen(POWER_CHANNEL_VCC2H);
        }
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "VDD2L")) != NULL)
        {
            if(strcasecmp(pcJsonItem[1]->valuestring, "DISABLE") == 0)
                vPowerClose(POWER_CHANNEL_VCC2L);
            else if(strcasecmp(pcJsonItem[1]->valuestring, "ENABLE") == 0)
                vPowerOpen(POWER_CHANNEL_VCC2L);
        }
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "VDDQ")) != NULL)
        {
            if(strcasecmp(pcJsonItem[1]->valuestring, "DISABLE") == 0)
                vPowerClose(POWER_CHANNEL_VCCQ);
            else if(strcasecmp(pcJsonItem[1]->valuestring, "ENABLE") == 0)
                vPowerOpen(POWER_CHANNEL_VCCQ);
        }
    }

    return 0;
}

int8_t cShellJsonCallbackVoltage(cJSON *pjsonJsonRoot)
{
    cJSON *pcJsonItem[3] = {NULL}, *pcJsonItemNew = NULL, *pcJsonArray = NULL;
    int32_t iArraySize = 0, i = 0;
    VoltageInfoType *ptypeVoltageInfoHandle = ptypeVoltageInfoGet();

    if((pcJsonItem[0] = cJSON_GetObjectItem(pjsonJsonRoot, "Set")) != NULL)
    {
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "Real")) != NULL)
        {
            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "All")) != NULL)
                cVoltageSet(VOLTAGE_CHANNEL_ALL, pcJsonItem[2]->valuedouble);

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD1")) != NULL)
                cVoltageSet(VOLTAGE_CHANNEL_VDD1, pcJsonItem[2]->valuedouble);

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD2H")) != NULL)
                cVoltageSet(VOLTAGE_CHANNEL_VDD2H, pcJsonItem[2]->valuedouble);

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD2L")) != NULL)
                cVoltageSet(VOLTAGE_CHANNEL_VDD2L, pcJsonItem[2]->valuedouble);

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDDQ")) != NULL)
                cVoltageSet(VOLTAGE_CHANNEL_VDDQ, pcJsonItem[2]->valuedouble);
        }

        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "LimitMax")) != NULL)
        {
            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "All")) != NULL)
            {
                ptypeVoltageInfoHandle->vdd1.limitMax  = pcJsonItem[2]->valuedouble;
                ptypeVoltageInfoHandle->vdd2H.limitMax = pcJsonItem[2]->valuedouble;
                ptypeVoltageInfoHandle->vdd2L.limitMax = pcJsonItem[2]->valuedouble;
                ptypeVoltageInfoHandle->vddQ.limitMax  = pcJsonItem[2]->valuedouble;
            }

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD1")) != NULL)
                ptypeVoltageInfoHandle->vdd1.limitMax = pcJsonItem[2]->valuedouble;

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD2H")) != NULL)
                ptypeVoltageInfoHandle->vdd2H.limitMax = pcJsonItem[2]->valuedouble;

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD2L")) != NULL)
                ptypeVoltageInfoHandle->vdd2L.limitMax = pcJsonItem[2]->valuedouble;

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDDQ")) != NULL)
                ptypeVoltageInfoHandle->vddQ.limitMax = pcJsonItem[2]->valuedouble;
        }

        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "LimitMin")) != NULL)
        {
            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "All")) != NULL)
            {
                ptypeVoltageInfoHandle->vdd1.limitMin = pcJsonItem[2]->valuedouble;
                ptypeVoltageInfoHandle->vdd2H.limitMin = pcJsonItem[2]->valuedouble;
                ptypeVoltageInfoHandle->vdd2L.limitMin = pcJsonItem[2]->valuedouble;
                ptypeVoltageInfoHandle->vddQ.limitMin = pcJsonItem[2]->valuedouble;
            }

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD1")) != NULL)
                ptypeVoltageInfoHandle->vdd1.limitMin = pcJsonItem[2]->valuedouble;

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD2H")) != NULL)
                ptypeVoltageInfoHandle->vdd2H.limitMin = pcJsonItem[2]->valuedouble;

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD2L")) != NULL)
                ptypeVoltageInfoHandle->vdd2L.limitMin = pcJsonItem[2]->valuedouble;

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDDQ")) != NULL)
                ptypeVoltageInfoHandle->vddQ.limitMin = pcJsonItem[2]->valuedouble;
        }
    }

    if((pcJsonItem[0] = cJSON_GetObjectItem(pjsonJsonRoot, "Get")) != NULL)
    {
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "Real")) != NULL)
        {
            /* 新增元素 */
            pcJsonItemNew = cJSON_CreateObject();
            cJSON_AddItemToObject(pcJsonItem[0], pcJsonItem[1]->string, pcJsonItemNew);

            /* 遍历 */
            iArraySize = cJSON_GetArraySize(pcJsonItem[1]);
            for(i = 0; i < iArraySize; ++i)
            {
                pcJsonArray = cJSON_GetArrayItem(pcJsonItem[1], i);

                if(strcasecmp(pcJsonArray->valuestring, "VDD1") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeVoltageInfoHandle->vdd1.get);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2H") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeVoltageInfoHandle->vdd2H.get);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2L") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeVoltageInfoHandle->vdd2L.get);

                else if(strcasecmp(pcJsonArray->valuestring, "VDDQ") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeVoltageInfoHandle->vddQ.get);
            }

            /* 删除旧数组元素 */
            cJSON_DeleteItemFromObject(pcJsonItem[0], pcJsonItem[1]->string);
        }

        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "Filter")) != NULL)
        {
            /* 新增元素 */
            pcJsonItemNew = cJSON_CreateObject();
            cJSON_AddItemToObject(pcJsonItem[0], pcJsonItem[1]->string, pcJsonItemNew);

            /* 遍历 */
            iArraySize = cJSON_GetArraySize(pcJsonItem[1]);
            for(i = 0; i < iArraySize; ++i)
            {
                pcJsonArray = cJSON_GetArrayItem(pcJsonItem[1], i);

                if(strcasecmp(pcJsonArray->valuestring, "VDD1") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeVoltageInfoHandle->vdd1.getFilter);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2H") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeVoltageInfoHandle->vdd2H.getFilter);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2L") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeVoltageInfoHandle->vdd2L.getFilter);

                else if(strcasecmp(pcJsonArray->valuestring, "VDDQ") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeVoltageInfoHandle->vddQ.getFilter);
            }

            /* 删除旧数组元素 */
            cJSON_DeleteItemFromObject(pcJsonItem[0], pcJsonItem[1]->string);
        }

        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "Error")) != NULL)
        {
            /* 新增元素 */
            pcJsonItemNew = cJSON_CreateObject();
            cJSON_AddItemToObject(pcJsonItem[0], pcJsonItem[1]->string, pcJsonItemNew);

            /* 遍历 */
            iArraySize = cJSON_GetArraySize(pcJsonItem[1]);
            for(i = 0; i < iArraySize; ++i)
            {
                pcJsonArray = cJSON_GetArrayItem(pcJsonItem[1], i);

                if(strcasecmp(pcJsonArray->valuestring, "VDD1") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeVoltageInfoHandle->vdd1.error);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2H") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeVoltageInfoHandle->vdd2H.error);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2L") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeVoltageInfoHandle->vdd2L.error);

                else if(strcasecmp(pcJsonArray->valuestring, "VDDQ") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeVoltageInfoHandle->vddQ.error);
            }

            /* 删除旧数组元素 */
            cJSON_DeleteItemFromObject(pcJsonItem[0], pcJsonItem[1]->string);
        }
    }

    return 0;
}

int8_t cShellJsonCallbackCurrent(cJSON *pjsonJsonRoot)
{
    cJSON *pcJsonItem[3] = {NULL}, *pcJsonItemNew = NULL, *pcJsonArray = NULL;
    int32_t iArraySize = 0, i = 0;
    CurrentInfoType *ptypeCurrentInfoHandle = ptypeCurrentInfoGet();

    /* 设置限制阈值 */
    if((pcJsonItem[0] = cJSON_GetObjectItem(pjsonJsonRoot, "Set")) != NULL)
    {
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "Limit")) != NULL)
        {
            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "All")) != NULL)
            {
                ptypeCurrentInfoHandle->vdd1.limit  = pcJsonItem[2]->valuedouble;
                ptypeCurrentInfoHandle->vdd2H.limit = pcJsonItem[2]->valuedouble;
                ptypeCurrentInfoHandle->vdd2L.limit = pcJsonItem[2]->valuedouble;
                ptypeCurrentInfoHandle->vddQ.limit  = pcJsonItem[2]->valuedouble;
            }

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD1")) != NULL)
                ptypeCurrentInfoHandle->vdd1.limit = pcJsonItem[2]->valuedouble;

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD2H")) != NULL)
                ptypeCurrentInfoHandle->vdd2H.limit = pcJsonItem[2]->valuedouble;

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD2L")) != NULL)
                ptypeCurrentInfoHandle->vdd2L.limit = pcJsonItem[2]->valuedouble;

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDDQ")) != NULL)
                ptypeCurrentInfoHandle->vddQ.limit = pcJsonItem[2]->valuedouble;
        }

        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "Max")) != NULL)
        {
            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "All")) != NULL)
            {
                ptypeCurrentInfoHandle->vdd1.getMax  = pcJsonItem[2]->valuedouble;
                ptypeCurrentInfoHandle->vdd2H.getMax = pcJsonItem[2]->valuedouble;
                ptypeCurrentInfoHandle->vdd2L.getMax = pcJsonItem[2]->valuedouble;
                ptypeCurrentInfoHandle->vddQ.getMax  = pcJsonItem[2]->valuedouble;
            }

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD1")) != NULL)
                ptypeCurrentInfoHandle->vdd1.getMax = pcJsonItem[2]->valuedouble;

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD2H")) != NULL)
                ptypeCurrentInfoHandle->vdd2H.getMax = pcJsonItem[2]->valuedouble;

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDD2L")) != NULL)
                ptypeCurrentInfoHandle->vdd2L.getMax = pcJsonItem[2]->valuedouble;

            if((pcJsonItem[2] = cJSON_GetObjectItem(pcJsonItem[1], "VDDQ")) != NULL)
                ptypeCurrentInfoHandle->vddQ.getMax = pcJsonItem[2]->valuedouble;
        }
    }

    if((pcJsonItem[0] = cJSON_GetObjectItem(pjsonJsonRoot, "Get")) != NULL)
    {
        /* 获取实时值 */
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "Real")) != NULL)
        {
            /* 新增元素 */
            pcJsonItemNew = cJSON_CreateObject();
            cJSON_AddItemToObject(pcJsonItem[0], pcJsonItem[1]->string, pcJsonItemNew);

            /* 遍历 */
            iArraySize = cJSON_GetArraySize(pcJsonItem[1]);
            for(i = 0; i < iArraySize; ++i)
            {
                pcJsonArray = cJSON_GetArrayItem(pcJsonItem[1], i);

                if(strcasecmp(pcJsonArray->valuestring, "VDD1") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vdd1.get);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2H") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vdd2H.get);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2L") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vdd2L.get);

                else if(strcasecmp(pcJsonArray->valuestring, "VDDQ") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vddQ.get);
            }

            /* 删除旧数组元素 */
            cJSON_DeleteItemFromObject(pcJsonItem[0], pcJsonItem[1]->string);
        }

        /* 获取滤波后的值 */
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "Filter")) != NULL)
        {
            /* 新增元素 */
            pcJsonItemNew = cJSON_CreateObject();
            cJSON_AddItemToObject(pcJsonItem[0], pcJsonItem[1]->string, pcJsonItemNew);

            /* 遍历 */
            iArraySize = cJSON_GetArraySize(pcJsonItem[1]);
            for(i = 0; i < iArraySize; ++i)
            {
                pcJsonArray = cJSON_GetArrayItem(pcJsonItem[1], i);

                if(strcasecmp(pcJsonArray->valuestring, "VDD1") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vdd1.getFilter);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2H") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vdd2H.getFilter);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2L") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vdd2L.getFilter);

                else if(strcasecmp(pcJsonArray->valuestring, "VDDQ") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vddQ.getFilter);
            }

            /* 删除旧数组元素 */
            cJSON_DeleteItemFromObject(pcJsonItem[0], pcJsonItem[1]->string);
        }

        /* 获取最大值 */
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "Max")) != NULL)
        {
            /* 新增元素 */
            pcJsonItemNew = cJSON_CreateObject();
            cJSON_AddItemToObject(pcJsonItem[0], pcJsonItem[1]->string, pcJsonItemNew);

            /* 遍历 */
            iArraySize = cJSON_GetArraySize(pcJsonItem[1]);
            for(i = 0; i < iArraySize; ++i)
            {
                pcJsonArray = cJSON_GetArrayItem(pcJsonItem[1], i);

                if(strcasecmp(pcJsonArray->valuestring, "VDD1") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vdd1.getMax);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2H") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vdd2H.getMax);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2L") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vdd2L.getMax);

                else if(strcasecmp(pcJsonArray->valuestring, "VDDQ") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vddQ.getMax);
            }

            /* 删除旧数组元素 */
            cJSON_DeleteItemFromObject(pcJsonItem[0], pcJsonItem[1]->string);
        }

        /* 获取最大值 */
        if((pcJsonItem[1] = cJSON_GetObjectItem(pcJsonItem[0], "Error")) != NULL)
        {
            /* 新增元素 */
            pcJsonItemNew = cJSON_CreateObject();
            cJSON_AddItemToObject(pcJsonItem[0], pcJsonItem[1]->string, pcJsonItemNew);

            /* 遍历 */
            iArraySize = cJSON_GetArraySize(pcJsonItem[1]);
            for(i = 0; i < iArraySize; ++i)
            {
                pcJsonArray = cJSON_GetArrayItem(pcJsonItem[1], i);

                if(strcasecmp(pcJsonArray->valuestring, "VDD1") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vdd1.error);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2H") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vdd2H.error);

                else if(strcasecmp(pcJsonArray->valuestring, "VDD2L") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vdd2L.error);

                else if(strcasecmp(pcJsonArray->valuestring, "VDDQ") == 0)
                    cJSON_AddNumberToObject(pcJsonItemNew, pcJsonArray->valuestring, ptypeCurrentInfoHandle->vddQ.error);
            }

            /* 删除旧数组元素 */
            cJSON_DeleteItemFromObject(pcJsonItem[0], pcJsonItem[1]->string);
        }
    }

    return 0;
}

shellJsonHookFunctionType st_shellJsonHookFunctionGather[] =
{
    {
        "Current",
        cShellJsonCallbackCurrent,
    },
    {
        "Voltage",
        cShellJsonCallbackVoltage,
    },
    {
        "Power",
        cShellJsonCallbackPower,
    },
    {
        "Version",
        cShellJsonCallbackVersion,
    },
};

#define JSON_HOOK_FUNCTION_NUMBER (sizeof(st_shellJsonHookFunctionGather) / sizeof(shellJsonHookFunctionType))


int8_t cShellParserJson(uint8_t *pucStrings, uint32_t uiChannel)
{
    cJSON *pjsonRoot = NULL, *pjsonItem = NULL;
    char *pcJSONPrintUnformatted = (char *)pucStrings;
    uint32_t uiChannelOSPF = 0, i = 0;
    int8_t cError = 1;


    if(pucStrings == NULL)
        return 1;

    pjsonRoot = cJSON_Parse(pcJSONPrintUnformatted);
    if(pjsonRoot == NULL)
    {
        printfError("pcJSONPrintUnformatted cJSON_Parse error.\r\n");
        return 2;
    }

    /* 定义透传通道 */
    switch(uiChannel)
    {
        case NETWORK_CHANNEL_HARD_USB:   uiChannelOSPF = NETWORK_CHANNEL_HARD_SOC | NETWORK_CHANNEL_HARD_LOG; break;
        case NETWORK_CHANNEL_HARD_UPPER: uiChannelOSPF = NETWORK_CHANNEL_HARD_SOC | NETWORK_CHANNEL_HARD_LOG; break;

        case NETWORK_CHANNEL_HARD_SOC:   uiChannelOSPF = NETWORK_CHANNEL_HARD_USB | NETWORK_CHANNEL_HARD_UPPER; break;
        case NETWORK_CHANNEL_HARD_LOG:   uiChannelOSPF = NETWORK_CHANNEL_HARD_USB | NETWORK_CHANNEL_HARD_SOC | NETWORK_CHANNEL_HARD_UPPER; break;

        default: uiChannelOSPF = NETWORK_CHANNEL_HARD_LOG; printfError("cJsonParser Channel error.\r\n"); break;
    }

    /* 板子的JSON头是 "MCU" */
    if((pjsonItem = cJSON_GetObjectItem(pjsonRoot, "MCU")) != NULL)
    {
        /* 查找指令钩子函数 */
        for(i = 0; i < JSON_HOOK_FUNCTION_NUMBER; ++i)
        {
            if((pjsonItem = cJSON_GetObjectItem(pjsonItem, st_shellJsonHookFunctionGather[i].name)) != NULL)
            {
                /* 把指令与数据发送到解析函数 */
                cError = st_shellJsonHookFunctionGather[i].cShellJsonHookFunction(pjsonItem);

                break;
            }
        }
    }

    if(cError == 0)
    {
        /* 非格式化转换 */
        pcJSONPrintUnformatted = cJSON_PrintUnformatted(pjsonRoot);

        cNetWorkSendDatas(uiChannel, pcJSONPrintUnformatted, strlen(pcJSONPrintUnformatted));

        cJSON_free(pcJSONPrintUnformatted);
    }
    /* 透传 */
    else
    {
        cNetWorkSendDatas(uiChannelOSPF, pcJSONPrintUnformatted, strlen(pcJSONPrintUnformatted));
    }

    cJSON_Delete(pjsonRoot);

    return cError;
}

